#!/usr/bin/python
import re
import sys

def parseTrainFile(trainFile):
    trFile = open(trainFile,'r')
    
    trainDict = {}
    
    for linea in trFile:
        linea = linea.split(">")
        key = "".join(linea[0].strip(" []\n").split(','))
        val = linea[1].strip(" []\n").split(',')
        #print "key:",
        #print key,
        #print "val:",
        #print val
        trainDict[key] = val
    
    trFile.close()
    return trainDict

def parseTrainFileSeq(trainFile):
    trFile = open(trainFile,'r')
    
    trainList = []
    
    for linea in trFile:
        linea = linea.split(">")
        key = "".join(linea[0].strip(" []\n").split(','))
        val = linea[1].strip(" []\n").split(',')
        trainList.append((key,val))
    
    trFile.close()
    return trainList

def dataCheck(trainFile, dataFile):
    td = parseTrainFile(trainFile)
    
    inFile = open(dataFile,'r')
    
    wrongBit = 0
    wrongEntries = 0
    totEntries = 0
    for linea in inFile:
        totEntries += 1
        linea = linea.split(">")
        key = "".join(linea[0].strip(" []\n").split(','))
        val = linea[1].strip(" []\n").split(',')
        trVal = td[key]
        diffList = []
        for myData, trData, index in zip(val,trVal, range(len(val)+1)):
            if trData == '*':
                continue
            if trData != myData:
                diffList.append(index)
        wrongBit += len(diffList)        
        if len(diffList) != 0:
            wrongEntries += 1
            for index in diffList:
                val[index] =  "\033[41m" + val[index] + "\033[0m"
            print "-"*35
            keyStr = "[" + ",".join(list(key)) + "]>" 
            print keyStr + "[" + ",".join(val) + "]"
            print " "*len(keyStr) + "[" + ",".join(trVal) + "]"
    
    inFile.close()
    
    print "Good Entries: %d/%d [%f]" % ((totEntries-wrongEntries), totEntries, 100-100*wrongEntries/float(totEntries))
    print "Good Bits: %d/%d [%f]" % (totEntries*8-wrongBit, totEntries*8, 100 - 100*wrongBit/float(totEntries*8))

def dataCheckSeq(trainFile, dataFile):
    tl = parseTrainFileSeq(trainFile)
    
    inFile = open(dataFile,'r')
    
    wrongBit = 0
    wrongEntries = 0
    totEntries = 0
    fileLines = inFile.readlines()
    
    for trainElement,linea in zip(tl,fileLines):
        totEntries += 1
        linea = linea.split(">")
        key = "".join(linea[0].strip(" []\n").split(','))
        val = linea[1].strip(" []\n").split(',')
        trVal = trainElement[1]
        diffList = []
        for myData, trData, index in zip(val,trVal, range(len(val)+1)):
            if trData == '*':
                continue
            if trData != myData:
                diffList.append(index)
        wrongBit += len(diffList)        
        if len(diffList) != 0:
            wrongEntries += 1
        for index in diffList:
            val[index] =  "\033[41m" + val[index] + "\033[0m"
        print "-"*35
        keyStr = "[" + ",".join(list(key)) + "]>" 
        print keyStr + "[" + ",".join(val) + "]"
        print " "*len(keyStr) + "[" + ",".join(trVal) + "]"
    
    inFile.close()
    
    print "Good Entries: %d/%d [%f]" % ((totEntries-wrongEntries), totEntries, 100-100*wrongEntries/float(totEntries))
    print "Good Bits: %d/%d [%f]" % (totEntries*8-wrongBit, totEntries*8, 100 - 100*wrongBit/float(totEntries*8))

def main():
    if len(sys.argv)<3:
        print "Utilizzo:\ndataCheck [-s] trainFile dataFile"
        return
    if sys.argv[1] == "-s":
        dataCheckSeq(sys.argv[2],sys.argv[3])
    else:
        dataCheck(sys.argv[1],sys.argv[2])
    
if __name__ == "__main__":
    main()